import {service} from '@/utils/request'

// 仪表板统计数据接口
export interface DashboardStats {
  totalUsers: number
  totalDevices: number  // 添加设备总数
  onlineDevices: number
  todayTreatments: number
  completionRate: number
  activeUsers: number
  totalTreatments: number
}

// 治疗趋势数据接口
export interface TreatmentTrend {
  date: string
  total_treatments: number
  completed_treatments: number
  completion_rate: number
  avg_intensity: number
}

// 用户增长数据接口
export interface UserGrowth {
  date: string
  new_users: number
  active_users: number
  total_users: number
}

// 实时活动接口
export interface RealtimeActivity {
  id: number
  type: 'treatment' | 'device' | 'user'
  message: string
  time: string
  user_id?: number
  device_id?: string
}

/**
 * 获取仪表板统计数据
 */
export const getDashboardStats = () => {
  return service<DashboardStats>({
    url: '/dashboard/stats',
    method: 'get'
  })
}

/**
 * 获取治疗趋势数据
 * @param days 天数 (7, 30, 90)
 */
export const getTreatmentTrends = (days: string = '30') => {
  return service<TreatmentTrend[]>({
    url: '/dashboard/treatment-trends',
    method: 'get',
    params: { days }
  })
}

/**
 * 获取用户增长趋势
 * @param days 天数 (7, 30, 90)
 */
export const getUserTrends = (days: string = '30') => {
  return service<UserGrowth[]>({
    url: '/dashboard/user-trends',
    method: 'get',
    params: { days }
  })
}

/**
 * 获取设备状态分布
 */
export const getDeviceStatusDistribution = () => {
  return service({
    url: '/dashboard/device-status',
    method: 'get'
  })
}

/**
 * 获取治疗模式分布
 * @param days 统计天数
 */
export const getTreatmentModeDistribution = (days: string = '30') => {
  return service({
    url: '/dashboard/treatment-modes',
    method: 'get',
    params: { days }
  })
}

/**
 * 获取最近治疗记录
 * @param limit 记录数量
 */
export const getRecentTreatments = (limit: number = 10) => {
  return service({
    url: '/treatments',
    method: 'get',
    params: { 
      limit,
      sort_field: 'created_at',
      sort_order: 'desc'
    }
  })
}

/**
 * 获取实时活动日志
 * @param limit 记录数量
 */
export const getRealtimeActivities = (limit: number = 20) => {
  return service<RealtimeActivity[]>({
    url: '/dashboard/activities',
    method: 'get',
    params: { limit }
  })
}

/**
 * 获取用户活跃度统计
 * @param period 统计周期 (daily, weekly, monthly)
 */
export const getUserActivityStats = (period: string = 'daily') => {
  return service({
    url: '/dashboard/user-activity',
    method: 'get',
    params: { period }
  })
}

/**
 * 获取设备使用统计
 * @param days 统计天数
 */
export const getDeviceUsageStats = (days: string = '30') => {
  return service({
    url: '/dashboard/device-usage',
    method: 'get',
    params: { days }
  })
}

/**
 * 获取治疗效果统计
 * @param days 统计天数
 */
export const getTreatmentEffectivenessStats = (days: string = '30') => {
  return service({
    url: '/dashboard/treatment-effectiveness',
    method: 'get',
    params: { days }
  })
}

/**
 * 获取系统性能指标
 */
export const getSystemMetrics = () => {
  return service({
    url: '/dashboard/system-metrics',
    method: 'get'
  })
}

/**
 * 获取告警信息
 * @param level 告警级别 (info, warning, error)
 * @param limit 记录数量
 */
export const getAlerts = (level?: string, limit: number = 50) => {
  return service({
    url: '/dashboard/alerts',
    method: 'get',
    params: { level, limit }
  })
}

/**
 * 导出仪表板数据
 * @param type 导出类型 (excel, pdf, csv)
 * @param dateRange 日期范围
 */
export const exportDashboardData = (type: string = 'excel', dateRange?: [string, string]) => {
  return service({
    url: '/dashboard/export',
    method: 'post',
    data: {
      type,
      start_date: dateRange?.[0],
      end_date: dateRange?.[1]
    },
    responseType: 'blob'
  })
}